package com.ns.basic.dict.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ns.basic.dict.bo.DictBO;
import com.ns.basic.dict.bo.DictPageBO;
import com.ns.basic.dict.dto.DictPageDto;
import com.ns.basic.dict.entry.DictEntry;
import com.ns.basic.dict.service.IDictEntryService;
import com.ns.basic.dict.service.IDictItemEntryService;
import com.ns.basic.dict.vo.DictEntryVO;
import com.ns.core.annotations.ReqLog;
import com.ns.core.bo.IdsBO;
import com.ns.core.current.config.SysConst;
import com.ns.core.utils.StpUtil;
import com.ns.core.utils.sg.ResultError;
import com.ns.core.utils.sg.ResultOk;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * 字典对外接口
 *
 * @author lw
 * @since 2024/08/28
 */
@RestController
@RequestMapping("/dict/")
@RequiredArgsConstructor
@CrossOrigin
public class DictEntryController {

    private final IDictEntryService dictEntryService;

    private final IDictItemEntryService dictItemEntryService;

    /**
     * 增
     *
     * @param bo 参数
     */
    @PostMapping("add")
    @ReqLog(module = "字典模块", name = "增")
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = SysConst.PROJECT_TAG, key = "'" + SysConst.PROJECT_DICT + "'")
    public ResultOk add(@RequestBody DictBO bo) {
        DictEntry dictEntry = new DictEntry();
        BeanUtils.copyProperties(bo, dictEntry);
        dictEntry.setSort(dictEntryService.count() + 1);
        if (!dictEntryService.saveOrUpdate(dictEntry)) {
            throw new ResultError("添加失败！");
        }
        return ResultOk.getSuccessData(dictEntry.getId());
    }

    /**
     * 逻辑删
     *
     * @param id 参数
     */
    @GetMapping("delete")
    @ReqLog(module = "字典模块", name = "逻辑删")
    @CacheEvict(cacheNames = SysConst.PROJECT_TAG, key = "'" + SysConst.PROJECT_DICT + "'")
    public ResultOk delete(Long id) {
        return ResultOk.getByBoolean(dictEntryService.removeById(id));
    }

    /**
     * 删 - 根据id列表
     */
    @PostMapping("deleteByIds")
    @ReqLog(module = "字典模块", name = "批量删")
    @CacheEvict(cacheNames = SysConst.PROJECT_TAG, key = "'" + SysConst.PROJECT_DICT + "'")
    public ResultOk deleteByIds(@RequestBody IdsBO bo) {
        return ResultOk.getByBoolean(dictEntryService.removeBatchByIds(bo.getIds()));
    }

    /**
     * 改
     *
     * @param bo 参数
     */
    @PostMapping("update")
    @ReqLog(module = "字典模块", name = "改")
    @CacheEvict(cacheNames = SysConst.PROJECT_TAG, key = "'" + SysConst.PROJECT_DICT + "'")
    public ResultOk update(@RequestBody DictBO bo) {
        DictEntry dictEntry = new DictEntry();
        BeanUtils.copyProperties(bo, dictEntry);
        dictEntry.setModifier(StpUtil.getLoginUserId());
        return ResultOk.getByBoolean(dictEntryService.saveOrUpdate(dictEntry));
    }

    /**
     * 查
     *
     * @param id 参数
     */
    @GetMapping("getById")
    @ReqLog(module = "字典模块", name = "查")
    public ResultOk getById(Long id) {
        DictEntry dictEntry = dictEntryService.getById(id);
        DictEntryVO vo = new DictEntryVO();
        BeanUtils.copyProperties(dictEntry, vo);
        vo.setItems(dictItemEntryService.listByDictId(vo.getId()));
        return ResultOk.getSuccessData(vo);
    }

    /**
     * 查 - 集合
     */
    @PostMapping("getList")
    @ReqLog(module = "字典模块", name = "集合")
    public ResultOk getList(@RequestBody DictPageBO bo) {
        DictPageDto dto = new DictPageDto();
        BeanUtils.copyProperties(bo, dto);
        Page<DictEntryVO> page = dictEntryService.selectBy(dto);
        return ResultOk.getPageData(page.getTotal(), page.getRecords());
    }
}
